package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.progress.core.port.adapter.service.CallFileApiService;
import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

@Service
@AllArgsConstructor
public class CallFileAPIServiceImpl implements CallFileApiService {
    private final RemoteFileFeignApi remoteFileFeignApi;

    @Override
    @SneakyThrows
    public String convertToPdf(String inputFileKey) {
        // 将文件转换为pdf
        ResponseData<FileInfoResponse> convertFileToPdfWithFileKeyResponseData = remoteFileFeignApi.postConvertFileToPdfWithFileKeyNoSign(inputFileKey);
        if (!convertFileToPdfWithFileKeyResponseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, convertFileToPdfWithFileKeyResponseData.getApplication(), String.valueOf(convertFileToPdfWithFileKeyResponseData.getErrorMsg()));
        }

        return convertFileToPdfWithFileKeyResponseData.getData().getSignKey();
    }

    @Override
    @SneakyThrows
    public String convertToPdfKey(String inputFileKey) {
        // 将文件转换为pdf
        ResponseData<FileInfoResponse> convertFileToPdfWithFileKeyResponseData = remoteFileFeignApi.postConvertFileToPdfWithFileKey(inputFileKey);
        if (!convertFileToPdfWithFileKeyResponseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, convertFileToPdfWithFileKeyResponseData.getApplication(), String.valueOf(convertFileToPdfWithFileKeyResponseData.getErrorMsg()));
        }

        return convertFileToPdfWithFileKeyResponseData.getData().getSignKey();
    }

    @Override
    @SneakyThrows
    public String uploadFile(File inputFile) {
        // 文件转MultipartFile
        FileItem fileItem = new DiskFileItem("file", Files.probeContentType(inputFile.toPath()), false, inputFile.getName(), (int) inputFile.length(), inputFile.getParentFile());
        try (OutputStream outputStream = fileItem.getOutputStream()) {
            outputStream.write(Files.readAllBytes(Paths.get(inputFile.getAbsolutePath())));
        }
        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

        // 上传文件
        ResponseData<FileInfoResponse> fileUpLoadResponseData = remoteFileFeignApi.fileUpLoad(multipartFile);
        if (!fileUpLoadResponseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, fileUpLoadResponseData.getApplication(), String.valueOf(fileUpLoadResponseData.getErrorMsg()));
        }

        return fileUpLoadResponseData.getData().getSignKey();
    }
}
